package tools;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.NonNull;
import org.springframework.web.client.RestTemplate;

import javax.xml.ws.Response;
import java.net.*;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 获取IP工具类
 */
public class IpUtils {

    public static final String IP_UNKNOWN = "unknown ip";

    /**
     * 获取本机IP
     *
     * @return 本机IP
     */
    public static String getLocalIp() {
        String os = System.getProperty("os.name");
        return os.toLowerCase().startsWith("win") ? getLocalIpFromWindows() : getLocalIpFromLinux();
    }

    public static String getMacAddr() {
        String MacAddress = "";
        StringBuilder str = new StringBuilder();

        try {
            NetworkInterface nic = NetworkInterface.getByName("eth0");
            byte[] buf = nic.getHardwareAddress();
            int length = buf.length;

            for (int i = 0; i < length; ++i) {
                byte aBuf = buf[i];
                str.append(byteHEX(aBuf));
            }

            MacAddress = str.toString().toUpperCase();
        } catch (SocketException e) {
            e.printStackTrace();
        }

        return MacAddress;
    }

    /**
     * 获取Linux服务器的本地IP
     *
     * @return IP
     */
    private static String getLocalIpFromLinux() {
        String ip = IP_UNKNOWN;

        try {
            Enumeration enumeration = NetworkInterface.getNetworkInterfaces();

            while (enumeration.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) enumeration.nextElement();
                if (ni.getName().equals("eth0")) {
                    Enumeration addresses = ni.getInetAddresses();
                    while (true) {
                        if (!addresses.hasMoreElements()) {
                            break;
                        }

                        InetAddress ia = (InetAddress) addresses.nextElement();
                        if (!(ia instanceof Inet6Address)) {
                            ip = ia.getHostAddress();
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }

        if (IP_UNKNOWN.equals(ip)) {
            ip = getLocalIpFromWindows();
        }

        return ip;
    }

    private static String byteHEX(byte ib) {
        char[] Digit = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        char[] ob = new char[]{Digit[ib >>> 4 & 15], Digit[ib & 15]};
        return new String(ob);
    }

    /**
     * 获取Windows服务器的本地IP
     *
     * @return IP
     */
    private static String getLocalIpFromWindows() {
        String localIp;
        try {
            localIp = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            localIp = IP_UNKNOWN;
        }

        return localIp;
    }

    /**
     * 获取主机名
     *
     * @return 主机名
     */
    public static String getHostName() {
        String hostName;
        try {
            hostName = InetAddress.getLocalHost().getHostName();
        } catch (Exception e) {
            hostName = "hostname";
        }
        return hostName;
    }

    /**
     * 获取客户端IP
     *
     * @param request ServerHttpRequest
     * @return 客户端IP
     */
    public static String getClientIp(@NonNull ServerHttpRequest request) {
        String result = null;
        HttpHeaders headers = request.getHeaders();
        if (headers.getFirst("X-Forwarded-For") != null) {
            result = headers.getFirst("X-Forwarded-For");
        } else if (headers.getFirst("Proxy-Client-IP") != null) {
            result = headers.getFirst("Proxy-Client-IP");
        } else if (headers.getFirst("WL-Proxy-Client-IP") != null) {
            result = headers.getFirst("WL-Proxy-Client-IP");
        } else if (headers.getFirst("HTTP_CLIENT_IP") != null) {
            result = headers.getFirst("HTTP_CLIENT_IP");
        } else if (headers.getFirst("X-Real-IP") != null) {
            result = headers.getFirst("X-Real-IP");
        }
        if (StringUtils.isBlank(result) || "unknown".equalsIgnoreCase(result)) {
            result = Objects.requireNonNull(request.getRemoteAddress()).getAddress().getHostAddress();
        }
        return result;
    }

    /**
     * 获取IP+主机名
     *
     * @return [IP+,+主机名]
     */
    public static String getIpWithBracketWrap() {
        return FormatUtils.wrapStringWithBracket(getLocalIp() + "," + getHostName());
    }

//    public static void main(String[] args) {
//        System.out.println(getLocalIpFromWindows());
//        System.out.println(getLocalIpFromLinux());
//    }


//    public static void main(String[] args) {
//        String a = "{\n" +
//                "    \"first\":\"step-1-100\",\n" +
//                "    \"steps\":{\n" +
//                "        \"step-1-100\":{\n" +
//                "            \"type\":\"isWireless\",\n" +
//                "            \"normal\":{\n" +
//                "                \"isEnd\":false,\n" +
//                "                \"isMerge\":false,\n" +
//                "                \"result\":\"摄像头无线接入\",\n" +
//                "                \"nextStep\":\"step-1-200\"\n" +
//                "            },\n" +
//                "\t\t\t\"abnormal\":{\n" +
//                "                \"isEnd\":false,\n" +
//                "                \"result\":\"摄像头为有线接入\",\n" +
//                "                \"nextStep\":\"step-1-0\"\n" +
//                "            },\n" +
//                "\t\t\t \"fail\":{\n" +
//                "                \"isEnd\":true,\n" +
//                "\t\t\t\t\"isMerge\":false,\n" +
//                "                \"result\":\"有线无线能力调用失败\",\n" +
//                "                \"isFail\":true,\n" +
//                "                \"result_type\":\"ICT综合判障\"\n" +
//                "            }\n" +
//                "        },\n" +
//                "\t\t\"step-1-0\":{\n" +
//                "            \"type\":\"ping\",\n" +
//                "            \"normal\":{\n" +
//                "                \"isEnd\":true,\n" +
//                "                \"isMerge\":false,\n" +
//                "                \"result\":\"摄像头PING正常\",\n" +
//                "                \"result_type\":\"ICT综合判障\"\n" +
//                "            },\n" +
//                "            \"abnormal\":{\n" +
//                "                \"isEnd\":false,\n" +
//                "                \"result\":\"摄像头PING异常\",\n" +
//                "                \"nextStep\":\"step-1-1\"\n" +
//                "            },\n" +
//                "            \"fail\":{\n" +
//                "                \"isEnd\":false,\n" +
//                "                \"result\":\"摄像头PING能力调用失败\",\n" +
//                "                \"isFail\":true,\n" +
//                "                \"nextStep\":\"step-1-1\"\n" +
//                "            }\n" +
//                "        },\n" +
//                "\t\t\"step-1-1\":{\n" +
//                "            \"type\":\"EIPtype\",\n" +
//                "            \"normal\":{\n" +
//                "                \"isEnd\":false,\n" +
//                "                \"result\":\"EIP线路\",\n" +
//                "                \"nextStep\":\"step-1-2A\"\n" +
//                "            },\n" +
//                "            \"abnormal\":{\n" +
//                "                \"isEnd\":false,\n" +
//                "                \"result\":\"VPN线路\",\n" +
//                "                \"nextStep\":\"step-1-2B\"\n" +
//                "            },\n" +
//                "            \"fail\":{\n" +
//                "                \"isEnd\":true,\n" +
//                "                \"result\":\"线路编号无法获取，能力调用失败\",\n" +
//                "                \"isFail\":true,\n" +
//                "                \"result_type\":\"ICT综合判障\"\n" +
//                "            }\n" +
//                "        },\n" +
//                "        \"step-1-2A\":{\n" +
//                "            \"type\":\"onu\",\n" +
//                "            \"normal\":{\n" +
//                "                \"isEnd\":true,\n" +
//                "                \"isMerge\":false,\n" +
//                "                \"result\":\"摄像头PING测告警，ONU正常\",\n" +
//                "                \"result_type\":\"终端故障\"\n" +
//                "            },\n" +
//                "            \"abnormal\":{\n" +
//                "                \"isEnd\":false,\n" +
//                "                \"result\":\"摄像头PING测告警，ONU异常\",\n" +
//                "                \"nextStep\":\"step-1-3A-1\"\n" +
//                "            },\n" +
//                "            \"fail\":{\n" +
//                "                \"isEnd\":false,\n" +
//                "                \"result\":\"ONU能力调用失败\",\n" +
//                "                \"isFail\":true,\n" +
//                "                \"nextStep\":\"step-1-3A-2\"\n" +
//                "            }\n" +
//                "        },\n" +
//                "       \"step-1-3A-1\":{\n" +
//                "            \"type\":\"olt\",\n" +
//                "            \"normal\":{\n" +
//                "                \"isEnd\":true,\n" +
//                "                \"isMerge\":true,\n" +
//                "                \"result\":\"摄像头告警，ONU异常，OLT正常\",\n" +
//                "                \"result_type\":\"接入网故障\"\n" +
//                "            },\n" +
//                "            \"abnormal\":{\n" +
//                "                \"isEnd\":true,\n" +
//                "                \"isMerge\":false,\n" +
//                "                \"result\":\"摄像头告警，OLT异常\",\n" +
//                "                \"result_type\":\"大网故障\"\n" +
//                "            },\n" +
//                "            \"fail\":{\n" +
//                "                \"isEnd\":true,\n" +
//                "                \"isMerge\":false,\n" +
//                "                \"result\":\"摄像头告警，OLT能力调用失败\",\n" +
//                "                \"isFail\":true,\n" +
//                "                \"result_type\":\"ICT综合判障\"\n" +
//                "            }\n" +
//                "        },\n" +
//                "       \"step-1-3A-2\":{\n" +
//                "            \"type\":\"olt\",\n" +
//                "            \"normal\":{\n" +
//                "                \"isEnd\":true,\n" +
//                "                \"isMerge\":false,\n" +
//                "                \"result\":\"摄像头ping测告警，ONU能力调用失败，OLT正常\",\n" +
//                "                \"result_type\":\"ICT综合判障\"\n" +
//                "            },\n" +
//                "            \"abnormal\":{\n" +
//                "                \"isEnd\":true,\n" +
//                "                \"isMerge\":false,\n" +
//                "                \"result\":\"摄像头ping测告警，ONU能力调用失败，OLT异常\",\n" +
//                "                \"result_type\":\"大网故障\"\n" +
//                "            },\n" +
//                "            \"fail\":{\n" +
//                "                \"isEnd\":true,\n" +
//                "                \"isMerge\":false,\n" +
//                "                \"result\":\"摄像头ping测告警，ONU能力调用失败，OLT能力调用失败\",\n" +
//                "                \"isFail\":true,\n" +
//                "                \"result_type\":\"ICT综合判障\"\n" +
//                "            }\n" +
//                "        },\n" +
//                "        \"step-1-2B\":{\n" +
//                "            \"type\":\"VPN网关\",\n" +
//                "            \"normal\":{\n" +
//                "                \"isEnd\":true,\n" +
//                "                \"isMerge\":false,\n" +
//                "                \"result\":\"摄像头PING测告警，网关通\",\n" +
//                "                \"result_type\":\"终端故障\"\n" +
//                "            },\n" +
//                "            \"abnormal\":{\n" +
//                "                \"isEnd\":true,\n" +
//                "                \"isMerge\":true,\n" +
//                "                \"result\":\"摄像头PING测告警，网关不通\",\n" +
//                "                \"result_type\":\"接入网故障\"\n" +
//                "            },\n" +
//                "            \"fail\":{\n" +
//                "                \"isEnd\":true,\n" +
//                "                \"result\":\"摄像头PING测告警，网关能力调用失败\",\n" +
//                "                \"isFail\":true,\n" +
//                "                \"result_type\":\"ICT综合判障\"\n" +
//                "            }\n" +
//                "        },\n" +
//                "        \"step-1-200\":{\n" +
//                "            \"type\":\"isOnTheNet\",\n" +
//                "            \"normal\":{\n" +
//                "\t\t\t\t\"isEnd\":false,\n" +
//                "\t\t\t\t\"result\":\"该用户在网\",\n" +
//                "\t\t\t\t\"nextStep\":\"step-1-300\"\n" +
//                "            },\n" +
//                "\t\t\t\"nonumber\":{\n" +
//                "\t\t\t\t\"isEnd\":false,\n" +
//                "\t\t\t\t\"result\":\"该用户查询不到\",\n" +
//                "\t\t\t\t\"isFail\":false,\n" +
//                "\t\t\t\t\"nextStep\":\"step-1-300\"\n" +
//                "\t\t\t},\n" +
//                "\t\t\t\"abnormal\":{\n" +
//                "\t\t\t\t\"isEnd\":true,\n" +
//                "\t\t\t\t\"isMerge\":false,\n" +
//                "\t\t\t\t\"result\":\"用户4G/5G物联网卡停机或拆机，原因程序获取\",\n" +
//                "\t\t\t\t\"isFail\":false,\n" +
//                "\t\t\t\t\"result_type\":\"移动卡状态异常\"\n" +
//                "\t\t\t},\n" +
//                "\t\t\t\"fail\":{\n" +
//                "\t\t\t\t\"isEnd\":true,\n" +
//                "\t\t\t\t\"isMerge\":false,\n" +
//                "\t\t\t\t\"result\":\"用户在网状态能力调用失败\",\n" +
//                "\t\t\t\t\"isFail\":true,\n" +
//                "\t\t\t\t\"result_type\":\"ICT综合判障\"\n" +
//                "\t\t\t}\n" +
//                "\t\t},\n" +
//                "\t\t\"step-1-300\":{\n" +
//                "\t\t\t\"type\":\"isOnLine\",\n" +
//                "\t\t\t\"normal\":{\n" +
//                "\t\t\t\t\"isEnd\":true,\n" +
//                "\t\t\t\t\"isMerge\":false,\n" +
//                "\t\t\t\t\"result\":\"无线网络正常，移动卡状态正常\",\n" +
//                "\t\t\t\t\"isFail\":false,\n" +
//                "\t\t\t\t\"result_type\":\"终端故障\"\n" +
//                "\t\t\t},\n" +
//                "\t\t\t\"nonumber\":{\n" +
//                "\t\t\t\t\"isEnd\":true,\n" +
//                "\t\t\t\t\"isMerge\":false,\n" +
//                "\t\t\t\t\"result\":\"移动卡号查询不到\",\n" +
//                "\t\t\t\t\"isFail\":false,\n" +
//                "\t\t\t\t\"result_type\":\"ICT综合判障\"\n" +
//                "\t\t\t},\n" +
//                "\t\t\t\"abnormal\":{\n" +
//                "\t\t\t\t\"isEnd\":true,\n" +
//                "\t\t\t\t\"isMerge\":false,\n" +
//                "\t\t\t\t\"result\":\"移动卡离线，摄像机断电或者移动卡模组故障\",\n" +
//                "\t\t\t\t\"isFail\":false,\n" +
//                "\t\t\t\t\"result_type\":\"终端故障\"\n" +
//                "\t\t\t},\n" +
//                "\t\t\t\"fail\":{\n" +
//                "\t\t\t\t\"isEnd\":true,\n" +
//                "\t\t\t\t\"isMerge\":false,\n" +
//                "\t\t\t\t\"result\":\"4G5G状态能力调用失败\",\n" +
//                "\t\t\t\t\"isFail\":true,\n" +
//                "\t\t\t\t\"result_type\":\"ICT综合判障\"\n" +
//                "\t\t\t}\n" +
//                "\t\t}\n" +
//                "\t}\n" +
//                "}";
//
//        JSONObject result = JSONUtil.parseObj(a);
////        String step = result.getStr("first");
////
////
////
////        JSONObject stepJson = JSONUtil.parseObj(result.getByPath("steps." + "step-1-3A-2"));//配置步骤总节点
////        String type = stepJson.getStr("type");//当前步骤名称
////        if (StringUtils.equals(type,"onu")){
////
////        }
////        System.out.println(step);
////        System.out.println(type);
////        System.out.println(stepJson);
////        System.out.println(stepJson.getStr("normal"));
//
//        Map<String,String> map = JSON.parseObject(JSONUtil.parseObj(a).getStr("steps"),Map.class);
//
//        JSONUtil.parseObj(a).getStr("steps");
//
////        JSONPath.read(a,'$..type')
//
//
//    }

    public static void main(String[] args) {
        onuDevStatusNewQuery("EIP200061258");
        onuDevStatusNewQuery("EIP200049371");
        onuDevStatusNewQuery("EIP200049385");
        onuDevStatusNewQuery("EIP200082094");
        onuDevStatusNewQuery("EIP200046403");
        onuDevStatusNewQuery("EIP200053110");
        onuDevStatusNewQuery("EIP100573819");
        onuDevStatusNewQuery("EIP200043134");
        onuDevStatusNewQuery("EIP200034599");
        onuDevStatusNewQuery("EIP200055044");
        onuDevStatusNewQuery("EIP200041208");
        onuDevStatusNewQuery("EIP200045317");
        onuDevStatusNewQuery("EIP20042350");
        onuDevStatusNewQuery("EIP200091866");





    }





    public static String onuDevStatusNewQuery(String strEIP) {
        String strOnuQueryStatusAppkey = "[B@5b3228b6";
        String strOnuQueryStatusServcode = "021.0001.0.ITMS_CpeOnline.ITMS_CpeOnline";
        String strOnuQueryStatusSender = "00102";
        String strOnuQueryStatusUrl = "http://10.4.9.245:8280/inmesb/service";

        JSONObject responseJson = JSONUtil.parseObj("{\"oltStatusCode\": 2,\"oltStatusData\": \"0\",\"onuStatusCode\": 2,\"onuStatusData\": \"0\",\"onuLightCode\": 2,\"onuTxPower\": \"0\",\"onuRxPower\": \"0\",\"onuStatus\": \"能力调用失败\"}");
        Response<JSONObject> response;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String requestserial = format.format(new Date());
            //String loid=jsonOltDevResult.getStr("loid");
            //String strBody = "{\"sysname\":\"QZ\",\"params\":[],\"infname\":\"ITMS-CpeOnline\",\"requestserial\":\"" + requestserial + "\",\"LOID\":\"0068694063\",\"ADNO\":\"" + strEIP + "\",\"authinfo\":\"xtadmin\"}";
            String strBody = "{\"sysname\":\"QZ\",\"params\":[],\"infname\":\"ITMS-CpeOnline\",\"requestserial\":\"" + requestserial + "\",\"LOID\":\"\",\"ADNO\":\"" + strEIP + "\",\"authinfo\":\"xtadmin\"}";
            HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
//            requestFactory.setConnectionRequestTimeout(60000);
            requestFactory.setConnectTimeout(600000);
            requestFactory.setReadTimeout(300000);

            RestTemplate restTemplate = new RestTemplate(requestFactory);

            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmm");
//            SimpleDateFormat timeFormatEx = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            Date date = new Date();
            String nowStr = timeFormat.format(date);
            String authCode = getMD5((strOnuQueryStatusAppkey + strOnuQueryStatusServcode + strOnuQueryStatusSender + nowStr).getBytes());//AuthCode算法：appKey + servCode + sender + time进行32位md5加密（不要+号
            //
            //构造webservice请求参数
            StringBuffer soapRequestData = new StringBuffer();
            soapRequestData.append("<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:e2e=\"http://e2e.shtel\">\n")
                    .append("   <soapenv:Header>\n")
                    .append("     <Esb>\n")
                    .append("      <Route>\n")
                    .append("      <Sender>00102</Sender>\n")
                    .append("      <ServCode>" + strOnuQueryStatusServcode + "</ServCode>\n")
                    .append("       <MsgId>000000001010111</MsgId>\n")
                    .append("       <Time>" + nowStr + "</Time>\n")
                    .append("        <TransId/>\n")
                    .append("        <Version>V0.1</Version>\n")
                    .append("        <AuthCode>" + authCode + "</AuthCode>\n")
                    .append("        <AuthType>NONE</AuthType>\n")
                    .append("        <MsgType/>\n")
                    .append("        <CarryType/>\n")
                    .append("        <ServTestFlag/>\n")
                    .append("          <MsgType/>\n")
                    .append("         </Route>\n")
                    .append("       </Esb>\n")
                    .append("   </soapenv:Header>\n")
                    .append("   <soapenv:Body>\n")
                    .append("    <e2e:ItmsService soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n")
                    .append("         <JSONStringParams xsi:type=\"xsd:string\">" + strBody + "</JSONStringParams>\n")
                    .append("      </e2e:ItmsService>\n")
                    .append("   </soapenv:Body>\n")
                    .append("</soapenv:Envelope>");
            String httpBody = soapRequestData.toString();
            //构造http请求头
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("text/xml;charset=UTF-8");
            headers.setContentType(type);
            headers.add("Pragma:", "no-cache");
            headers.add("Cache-Control", "no-cache");
            headers.add("Content-Length", String.valueOf(httpBody.length()));
            headers.add("SOAPAction", "");
            List<Charset> acceptCharset = new ArrayList<>();
            acceptCharset.add(Charset.forName("UTF-8"));
            headers.setAcceptCharset(acceptCharset);
            headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

            HttpEntity<String> formEntity = new HttpEntity<>(httpBody, headers);
//            System.out.println("[onuDevStatusQuery] query={}" + soapRequestData.toString());

            String resultStr = null;
//            try {

            resultStr = restTemplate.postForObject(strOnuQueryStatusUrl, formEntity, String.class);
//            } catch (RestClientException e) {
//                e.printStackTrace();
//                if (count < number) {
//                    count++;
//                    logger.warn("[onuDevStatusCount] 调用次数count={}", count);
//                    return onuDevStatusNewQuery(strEIP);
//                } else {
//                    responseJson.put("onuStatus", "能力调用失败");
//                    return Response.builder(1, "error", responseJson);
//                }
//
//            }
            resultStr = StringEscapeUtils.unescapeXml(resultStr);
//            System.out.println("[onuDevStatusQuery] return={}" + resultStr);
            //获取真正的结果
            String strResultJson = StringUtils.substringBetween(resultStr, "<ItmsServiceReturn xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">", "</ItmsServiceReturn>");

            String replace = strResultJson.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Body><ns1:ItmsServiceResponse soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://e2e.shtel\"><ItmsServiceReturn xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">", "");

//            System.out.println(strEIP+":"+JSONPath.read(replace, "$.itmsRespInfoList[0].onlinestatus"));
            System.out.println(strEIP+":"+replace);

            return replace;
        }catch (Exception e){
            System.out.println("error");
        }
        return "strResultJson";
    }


    public static String getMD5(byte[] bytes) {
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        char[] str = new char[16 * 2];
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            md.update(bytes);
            byte[] tmp = md.digest();
            int k = 0;
            for (int i = 0; i < 16; i++) {
                byte byte0 = tmp[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(str);
    }
}
